package com.gengine.media.core.play
{
	import com.gengine.media.events.*;
	import com.gengine.media.interfaces.*;
	import flash.errors.*;
	import flash.events.*;
	import flash.media.*;
	import flash.net.*;
	import flash.utils.*;
	
	public class MP3PlayCore extends MediaPlayCore implements IMediaPlayCore
	{
		private var _sound:Sound;
		private var _context:SoundLoaderContext;
		private var _soundChanel:SoundChannel;
		private var _timer:Timer;
		private var _pauseTime:Number = 0;
		private var _isPlayStart:Boolean = false;
		
		public function MP3PlayCore(param1:String = null, param2:Number = 1000, param3:Boolean = false, param4:Number = 250, param5:Object = null)
		{
			var url:* = param1;
			var bufferTime:* = param2;
			var checkPolicyFile:* = param3;
			var updateDelay:* = param4;
			var callback:* = param5;
			super(url, bufferTime, callback);
			this._timer = new Timer(updateDelay);
			this._timer.addEventListener(TimerEvent.TIMER, this.playingStateTimerEventHandler);
			this._context = new SoundLoaderContext(bufferTime, checkPolicyFile);
			this._sound = new Sound();
			this._sound.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorEventHandler);
			this._sound.addEventListener(Event.OPEN, this.openEventHandler);
			this._sound.addEventListener(ProgressEvent.PROGRESS, this.progressEventHandler);
			this._sound.addEventListener(Event.COMPLETE, this.completeEventHandler);
			this._sound.addEventListener(Event.ID3, this.id3DataEventHandler);
			try
			{
				this._sound.load(new URLRequest(url), this._context);
			}
			catch (err:IOError)
			{
				trace("流加载IO错误：" + err.message);
				;
			}
			catch (err:SecurityError)
			{
				trace("流加载安全性错误：" + err.message);
			}
			
		}
		
		override public function set volume(param1:Number) : void
		{
			var _loc_2:SoundTransform = null;
			super.volume = param1;
			if (this._soundChanel)
			{
				_loc_2 = this._soundChanel.soundTransform;
				_loc_2.volume = _mute ? (0) : (param1);
				this._soundChanel.soundTransform = _loc_2;
			}
			
		}
		
		override public function play() : void
		{
			if (!playing)
			{
				super.play();
				this.soundPlayAction();
			}
			else
			{
				trace("声音正处于播放状态，无法重复进行播放动作！");
			}
			
		}
		
		override public function pause() : void
		{
			if (playing)
			{
				super.pause();
				this.soundStopAction();
				callbackFunction(MediaPlayEvent.PAUSE);
			}
			else
			{
				trace("声音正处于非播放状态，无法进行暂停播放动作！");
			}
			
		}
		
		override public function resume() : void
		{
			if (paused)
			{
				super.play();
				this.soundPlayAction(this._pauseTime);
				callbackFunction(MediaPlayEvent.RESUME);
			}
			else
			{
				trace("声音正处于非暂停状态，无法进行暂停播放动作！");
			}
			
		}
		
		override public function stop() : void
		{
			if (!stopped)
			{
				super.stop();
				this.soundStopAction(true);
				callbackFunction(MediaPlayEvent.STOP);
			}
			else
			{
				trace("声音正处于停止状态，无法重复进行停止播放动作！");
			}
			
		}
		
		override public function seek(param1:Number = 0) : void
		{
			var _loc_2:Boolean = false;
			if (!stopped)
			{
				if (param1 < 0)
				{
					param1 = 0;
					;
				}
				if (param1 >= _totalTime)
				{
					param1 = _totalTime - 0.001;
					;
				}
				_loc_2 = playing;
				if (_loc_2)
				{
					this.pause();
					;
				}
				this._pauseTime = param1 * 1000;
				callbackFunction(MediaPlayEvent.SEEK);
				if (_loc_2)
				{
					this.resume();
					;
				}
			}
			else
			{
				trace("声音正处于停止状态，无法进行播放进度搜索动作！");
			}
			
		}
		
		override public function seekPercent(param1:Number = 0) : void
		{
			var _loc_2:* = param1 * _totalTime;
			this.seek(_loc_2);
			
		}
		
		override public function dispose() : void
		{
			if (this._sound != null)
			{
				this._sound.removeEventListener(IOErrorEvent.IO_ERROR, this.ioErrorEventHandler);
				this._sound.removeEventListener(Event.OPEN, this.openEventHandler);
				this._sound.removeEventListener(ProgressEvent.PROGRESS, this.progressEventHandler);
				this._sound.removeEventListener(Event.COMPLETE, this.completeEventHandler);
				this._sound.removeEventListener(Event.ID3, this.id3DataEventHandler);
				this._sound = null;
			}
			if (this._timer != null)
			{
				this._timer.removeEventListener(TimerEvent.TIMER, this.playingStateTimerEventHandler);
				this._timer.stop();
				this._timer = null;
			}
			if (this._soundChanel != null)
			{
				this._soundChanel.removeEventListener(Event.SOUND_COMPLETE, this.soundCompleteEventHandler);
				this._soundChanel = null;
			}
			
		}
		
		private function soundPlayAction(param1:Number = 0) : void
		{
			this._timer.start();
			this._soundChanel = this._sound.play(param1);
			this._soundChanel.addEventListener(Event.SOUND_COMPLETE, this.soundCompleteEventHandler);
			this.volume = _volume;
			
		}
		
		private function soundStopAction(param1:Boolean = false) : void
		{
			var close:* = param1;
			this._timer.stop();
			this._soundChanel.stop();
			this._soundChanel.removeEventListener(Event.SOUND_COMPLETE, this.soundCompleteEventHandler);
			if (close)
			{
				this._pauseTime = 0;
				try
				{
					this._sound.close();
				}
				catch (err:IOError)
				{
				}
			}
			else
			{
				this._pauseTime = this._soundChanel.position;
			}
			this._soundChanel = null;
			
		}
		
		private function ioErrorEventHandler(event:IOErrorEvent) : void
		{
			callbackFunction(MediaPlayEvent.IO_ERROR, event.text);
			
		}
		
		private function openEventHandler(event:Event) : void
		{
			var _loc_2:* = _totalTime * 1000;
			if (this._context.bufferTime > _loc_2)
			{
				this._context.bufferTime = _loc_2;
				;
			}
			callbackFunction(MediaPlayEvent.OPEN);
			
		}
		
		private function progressEventHandler(event:ProgressEvent) : void
		{
			_loadPercentage = event.bytesLoaded / event.bytesTotal;
			callbackFunction(MediaPlayEvent.PROGRESS);
			
		}
		
		private function completeEventHandler(event:Event) : void
		{
			callbackFunction(MediaPlayEvent.COMPLETE);
			
		}
		
		private function id3DataEventHandler(event:Event) : void
		{
			_mediaData = this._sound.id3;
			_mediaData.type = "mp3";
			callbackFunction(MediaPlayEvent.INIT);
			
		}
		
		private function playingStateTimerEventHandler(event:TimerEvent) : void
		{
			_buffering = this._sound.isBuffering;
			_playheadTime = this._soundChanel.position / 1000;
			_totalTime = this._sound.bytesTotal / (this._sound.bytesLoaded / this._sound.length) / 1000;
			if (_buffering)
			{
				_bufferPercentage = this._sound.length / (this._soundChanel.position + _bufferTime);
				if (_bufferPercentage < 0)
				{
					_bufferPercentage = 0;
					;
				}
				callbackFunction(MediaPlayEvent.BUFFERING);
			}
			else
			{
				if (!this._isPlayStart && _playheadTime > 0)
				{
					this._isPlayStart = true;
					callbackFunction(MediaPlayEvent.PLAY);
				}
				callbackFunction(MediaPlayEvent.PLAYING);
			}
			
		}
		
		private function soundCompleteEventHandler(event:Event) : void
		{
			this.stop();
			callbackFunction(MediaPlayEvent.PLAY_COMPLETE);
			
		}
		
	}
}
